package com.ruoyi.wx.controller;

import java.io.File;
import java.io.IOException;
import java.util.*;


import io.jsonwebtoken.lang.Assert;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.wx.domain.Goods;
import com.ruoyi.wx.service.IGoodsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 商品信息Controller
 *
 * @author hxie
 * @date 2020-12-24
 */
@RestController
@RequestMapping("/wx/goods")
public class GoodsController extends BaseController
{
    @Autowired
    private IGoodsService goodsService;

    /**
     * 查询商品信息列表
     */
//    @PreAuthorize("@ss.hasPermi('wx:goods:list')")
    @GetMapping("/list")
    public TableDataInfo list(Goods goods)
    {
        startPage();
        List<Goods> list = goodsService.selectGoodsList(goods);
        return getDataTable(list);
    }

    /**
     * 导出商品信息列表
     */
//    @PreAuthorize("@ss.hasPermi('wx:goods:export')")
    @Log(title = "商品信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Goods goods)
    {
        List<Goods> list = goodsService.selectGoodsList(goods);
        ExcelUtil<Goods> util = new ExcelUtil<Goods>(Goods.class);
        return util.exportExcel(list, "goods");
    }

    /**
     * 获取商品信息详细信息
     */
//    @PreAuthorize("@ss.hasPermi('wx:goods:query')")
    @GetMapping(value = "/{goodsId}")
    public AjaxResult getInfo(@PathVariable("goodsId") String goodsId)
    {
        return AjaxResult.success(goodsService.selectGoodsById(goodsId));
    }

    /**
     * 新增商品信息
     */
//    @PreAuthorize("@ss.hasPermi('wx:goods:add')")
    @Log(title = "商品信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Goods goods)
    {

        return toAjax(goodsService.insertGoods(goods));
    }

    /**
     * 修改商品信息
     */
//    @PreAuthorize("@ss.hasPermi('wx:goods:edit')")
    @Log(title = "商品信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Goods goods)
    {
        return toAjax(goodsService.updateGoods(goods));
    }

    /**
     * 删除商品信息
     */
//    @PreAuthorize("@ss.hasPermi('wx:goods:remove')")
    @Log(title = "商品信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{goodsIds}")
    public AjaxResult remove(@PathVariable String[] goodsIds)
    {
        return toAjax(goodsService.deleteGoodsByIds(goodsIds));
    }

    /**
     * 上传图片
     * @param file
     * @return
     * @throws IOException
     */
    @Log(title = "上传图片",businessType = BusinessType.INSERT)
    @PostMapping(value = "/upload",produces = {"application/json;charset=UTF-8"})
    public Map<String ,String> picture(@RequestParam("file") MultipartFile file) throws IOException{
        logger.info("上传图片");
        Assert.notNull(file,"文件不能为空!");
        //获取当前工作目录
        String projectPath =System.getProperty("user.dir");

        String oldName =file.getOriginalFilename();
        //通过UUID生成唯一文件名
        String newName = UUID.randomUUID()+oldName.substring(oldName.lastIndexOf("."));
        String savePath =projectPath+"/ruoyi-wx/src/main/resources/static/image/goods/"+newName;
        file.transferTo(new File(savePath));
        Map<String,String> map=new HashMap<>();
        map.put("name",newName);
        map.put("url","image/goods/"+newName);
        return map;
    }

    /**
     * 首页“为你推荐”
     * @return
     */
    @RequestMapping("/recommend")
    public List<Goods> recommend(){
        List<Goods> goodsList=goodsService.recommend();
        return goodsList;
    }
    /**
     * 根据id返回对应的奶茶
     * @param goodsId
     * @return
     */
    @RequestMapping("/selectOneMilktea")
    public Goods selectOneMilktea(String goodsId){
        Goods goods=goodsService.selectOneMilktea(goodsId);
        return goods;
    }

    /**
     * 返回全部奶茶
     * @return
     */
    @RequestMapping("/selectAllMilktea")
    public List<Goods> selectAllMilktea(){
        List<Goods> goodsList=goodsService.selectAllMilktea();
        return goodsList;
    }

    /**
     * 返回对应分类的奶茶
     */
    @RequestMapping("/selectByType")
    public List<Goods> selectByType(String kindsName){
        List<Goods> goodsList=goodsService.selectByType(kindsName);
        return goodsList;
    }

    /**
     * 返回对应分类的奶茶
     */
    @RequestMapping("/selectAllByType")
    public List<List<Goods>> selectAllByType(String kindsName){
        List<List<Goods>> res=new ArrayList<List<Goods>>();
        for (int i = 0; i < 4; i++) {
            List<Goods> temp = selectByType(Integer.toString(i) );
            res.add(temp);
        }
        return res;
    }

    /**
     * 返回奶茶的种类数
     */
    @RequestMapping("/countType")
    public int countType() {
        return goodsService.countType();
    }

    /**
     * 返回奶茶的种类数
     */
    @RequestMapping("/countMilktea")
    public int countMilktea() {
        return goodsService.countMilktea();
    }

}
